<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;
class UserController extends Controller
{
    //add
    public function add()
    {
    	return view('admin.user.add');
    }

     //insert
    public function insert(Request $request)
    {
    	// dd($request -> all());
    	$data = $request -> except('_token','repassword');
    	//验证
    	$this->validate($request, [
        'username' => 'required|min:6|max:18',
        'password' => 'required',
        'repassword' => 'required|same:password',
        'email' => 'required|email'
    ],[
    	'username.required' => '用户名不能为空',
    	'username.min' => '用户名不能少于6位',
    	'username.max' => '用户名不能大于18位',
    	'password.required' => '密码不能为空',
    	'repassword.required' => '确认密码不能为空',
    	'repassword.same' => '两次密码不一致',
    	'email.email' => '邮箱不合法',
    	'email.required' => '邮箱不能为空',
    ]);
    	//处理密码
    	$data['password'] = Crypt::encrypt($data['password']);
    	
    	//处理token
    	$data['token'] = str_random(50);
        //处理注册时间
        $data['ftime'] = time();
    	//状态
    	$data['status'] = 1;
    	
    	//执行添加
    	$res = DB::table('users_edit') -> insert($data);
        
    	//判断
    	if($res)
    	{
    		return redirect('/admin/user/back') -> with(['info' => '添加成功']);

    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }

    //前台用户
    //用户列表分页
    public function index(Request $request)
    {
    	$data = DB::table('users') 
    	->where('username','like','%'.$request -> input('keywords').'%')
    	->paginate($request -> input('num', 10));//每页显示条数
    	return view('admin.user.index', ['data' => $data, 'request' => $request -> all()]);
        
    }


//管理员用户信息逻辑处理
//-------------------------------------------------------------------------------------------------

    //管理员列表分页
    public function admin(Request $request)
    {
        $data = DB::table('users_admin') 
        ->where('username','like','%'.$request -> input('keywords').'%')
        ->paginate($request -> input('num', 10));//每页显示条数       
        return view('admin.user.admin', ['data' => $data, 'request' => $request -> all()]);
    }

    //编辑管理员信息
    public function edit_admin($id)
    {
        $data = DB::table('users_admin')->where('id', $id)->first();
        return view('admin.user.edit_admin', ['data' => $data]);
    }

    //执行管理员信息编辑
     public function update_admin(Request $request)
    {
        $data = $request -> except('_token');
        //验证
        $this->validate($request, [
        'username' => 'required|min:6|max:18',
        'email' => 'required|email'
    ],[
        'username.required' => '用户名不能为空',
        'username.min' => '用户名不能少于6位',
        'username.max' => '用户名不能大于18位',
        'email.email' => '邮箱不合法',
        'email.required' => '邮箱不能为空',
    ]);
                
        //执行修改
        $res = DB::table('users_admin') -> where('id', $data['id']) -> update($data);

        //判断
        if($res)
        {
            return redirect('/admin/user/admin') -> with(['info' => '修改管理员信息成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }

//-----------------------------------------------------------------------------------------------------------
// 后台工作账号逻辑处理
// ----------------------------------------------------------------------------------------------------------
    //后台用户列表分页
    public function back(Request $request)
    {
        $data = DB::table('users_edit') 
        ->where('username','like','%'.$request -> input('keywords').'%')
        ->paginate($request -> input('num', 10));//每页显示条数
        return view('admin.user.back', ['data' => $data, 'request' => $request -> all()]);        
    }

    //编辑工作信息
    public function edit_back($id)
    {
        $data = DB::table('users_edit')->where('id', $id)->first();
        return view('admin.user.edit_back', ['data' => $data]);
    }

    //执行工作账号信息编辑
     public function update_back(Request $request)
    {
        $data = $request -> except('_token');
        //验证
        $this->validate($request, [
        'username' => 'required|min:6|max:18',
        'email' => 'required|email'
    ],[
        'username.required' => '用户名不能为空',
        'username.min' => '用户名不能少于6位',
        'username.max' => '用户名不能大于18位',
        'email.email' => '邮箱不合法',
        'email.required' => '邮箱不能为空',
    ]);
                
        //执行修改
        $res = DB::table('users_edit') -> where('id', $data['id']) -> update($data);

        //判断
        if($res)
        {
            return redirect('/admin/user/back') -> with(['info' => '修改工作账号信息成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }

    //执行删除
    public function delete_back($id)
    {
        //执行删除
        $res = DB::table('users_edit') -> delete($id);
        if($res)
        {
            return redirect('/admin/user/back') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }   
    }
//------------------------------------------------------------------------------------------------------

    //用户列表状态按钮
    public function ajaxUpdate(Request $request)
    {
    	//获取id
    	$id = $request -> input('id');
    	//从数据库中查询id的status
    	$data = DB::table('users') -> select('status') -> where('id', $id) -> first();
    	
    	//判断
    	if($data -> status == 0)
    	{
    		$res = DB::table('users') -> where('id', $id) -> update(['status' => 1]);
    		if($res)
    		{
    			return response() -> json('1');
    		}else
    		{
    			return response() -> json('5');
    		}
    		
    	}else
    	{
    		$res = DB::table('users') -> where('id', $id) -> update(['status' => 0]);
    		if($res)
    		{
    			return response() -> json('0');
    		}else
    		{
    			return response() -> json('5');
    		}
    	}
    }

    //修改用户名
    public function ajaxName(Request $request)
    {
    	//查询数据库
    	$res = DB::table('users') -> where('username', $request -> input('username')) -> first();

    	if($res)
    	{
    		//用户已经存在 返回5
    		return response() -> json('5');
    	}else
    	{
    		$r = DB::table('users') -> where('id', $request -> id) -> update(['username' => $request -> username]);
    		if($r)
    		{
    			return response() -> json('0');
    		}else
    		{
    			return response() -> json('1');
    		}
    	}
    }
    //编辑用户信息
    public function edit($id)
    {
    	$data = DB::table('users')->where('id', $id)->first();
    	return view('admin.user.edit', ['data' => $data]);
    }
    //执行编辑
     public function update(Request $request)
    {
    	$data = $request -> except('_token');
    	//验证
    	$this->validate($request, [
        'username' => 'required|min:6|max:18',
        'email' => 'required|email'
    ],[
    	'username.required' => '用户名不能为空',
    	'username.min' => '用户名不能少于6位',
    	'username.max' => '用户名不能大于18位',
    	'email.email' => '邮箱不合法',
    	'email.required' => '邮箱不能为空',
    ]);
    	    	
    	//执行修改
    	$res = DB::table('users') -> where('id', $data['id']) -> update($data);

    	//判断
    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '修改成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }
    //执行删除
    public function delete($id)
    {
    	//执行删除
    	$res = DB::table('users') -> delete($id);
    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '删除成功']);
    	}else
    	{
    		return back() -> with(['info' => '删除失败']);
    	}	
    }
    //用户详情
     public function details($id)
    {
        //查询
        $res = DB::table('users') -> where('id', $id) -> first();
        if($res)
        {
             return view('admin.user.details',['data' => $res]);
        }else
        {
            return back() -> with(['info' => '获取用户信息失败']);
        }
        
    }
    
}
